<template>
  <div>
  <el-card >
    <el-card style="height: 80px" >
      <!-- 顶部面包屑标识与导航 -->
      <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
        <el-breadcrumb-item :to="{ path: '/' }">
          <i class="el-icon-s-promotion"></i> 后台管理
        </el-breadcrumb-item>
        <el-breadcrumb-item>产品管理</el-breadcrumb-item>
      </el-breadcrumb>
      <el-divider></el-divider>
    </el-card>
    <el-row  style="margin-top: 10px">
      <el-button type="primary">新增</el-button>
      <el-button type="info">导出</el-button>
    </el-row>
    <el-table
        :data="productData"
        style="width: 100%">
      <el-table-column
          prop="id"
          label="产品编号"
          width="100">
      </el-table-column>
      <el-table-column
          prop="name"
          label="产品名称">
      </el-table-column>
      <el-table-column
          prop="categoryName"
          label="产品类型">
      </el-table-column>
      <el-table-column
          prop="inventory"
          label="库存">
      </el-table-column>
      <el-table-column
          prop="sales"
          label="销量">
      </el-table-column>
      <el-table-column
          prop="date"
          label="日期">
      </el-table-column>
      <el-table-column
          prop="price"
          label="单价">
      </el-table-column>
      <el-table-column label="操作"  width="200px"
                       align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" circle size="mini"
                     @click="openEditDialog(scope.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle size="mini"
                     @click="openDeleteConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
    <!-- 修改数据的表单 -->
    <el-dialog title="编辑用户管理数据" :visible.sync="editFormVisible">
      <el-form :model="editForm"  label-width="120px">
        <el-form-item label="产品名称" prop="name">
          <el-input v-model="editForm.name"></el-input>
        </el-form-item>
        <el-form-item label="产品类型" prop="categoryName">
          <el-input v-model="editForm.categoryName"></el-input>
        </el-form-item>
        <el-form-item label="库存数量" prop="inventory">
          <el-input v-model="editForm.inventory"></el-input>
        </el-form-item>
        <el-form-item label="销量" prop="sales">
          <el-input v-model="editForm.sales"></el-input>
        </el-form-item>
        <el-form-item label="日期" prop="date">
          <el-input v-model="editForm.date"></el-input>
        </el-form-item>
        <el-form-item label="单价" prop="price">
          <el-input v-model="editForm.price"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleEdit()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Product",
  editFormVisible: false,
  editForm: {
    id: '',
    name: '',
    categoryName: '',
    inventory: '',
    sales: '',
    date: '',
    price: '',
  },
  data:function (){
    return {
      editFormVisible: false,
      editForm: {
        id: '',
        name: '',
        categoryName: '',
        inventory: '',
        sales: '',
        date: '',
        price: '',
      },
    }
  },
  methods:{
    // 弹出修改对话框
    openEditDialog(tableItem) {
      let url = 'http://localhost:8080/v1/admins/' + tableItem.id;
      console.log('url = ' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('localJwt')}})
          .get(url).then((response) => {
        let jsonResult = response.data;
        console.log('jsonResult ='+ jsonResult)
        if (jsonResult.code == 1) {
          this.editForm = jsonResult.data;
          this.editFormVisible = true;
        } else {
          let title = '操作失败';
          this.$alert(jsonResult.message, title, {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    },
    // 执行修改
    handleEdit() {
      let url = 'http://localhost:8080/v1/admins/' + this.editForm.id + '/update';
      console.log('url = ' + url);
      let formData = this.qs.stringify(this.editForm);
      console.log('formData = ' + formData);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('localJwt')}})
          .post(url, formData).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.code ==1) {
          this.$message({
            message: '修改标签成功！',
            type: 'success'
          });
          this.editFormVisible = false;
          this.loadRoleList();
        } else if (jsonResult.state == 4040) {
          let title = '操作失败';
          this.$alert(jsonResult.message, title, {
            confirmButtonText: '确定',
            callback: action => {
              this.editFormVisible = false;
            }
          });
        } else {
          let title = '操作失败';
          this.$alert(jsonResult.message, title, {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    },
    onSubmit() {
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('localJwt')}})
          .get("/v1/users/selectByAdmin").then((response)=> {
        this.userList=response.data.data;
      })
    },
    roleHandleEdit(index,user){
      console.log(index);
    },
    roleHandleDelete(){
      console.log(index);
    },
  }
}
</script>

<style scoped>

</style>